﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;


namespace DnProcessSimulateCommands.RenameTool.SplitButton;

[TemplatePart(Name = "PART_DropDown", Type = typeof (Button))]
[System.Windows.Markup.ContentProperty("Items")]
[DefaultProperty("Items")]
public class SplitButton : Button
{
  public static readonly DependencyProperty HorizontalOffsetProperty;
  public static readonly DependencyProperty IsContextMenuOpenProperty;
  public static readonly DependencyProperty ModeProperty;
  public static readonly DependencyProperty PlacementProperty;
  public static readonly DependencyProperty PlacementRectangleProperty;
  public static readonly DependencyProperty VerticalOffsetProperty;

  static SplitButton()
  {
    FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton)));
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.IsContextMenuOpenProperty = DependencyProperty.Register(nameof (IsContextMenuOpen), typeof (bool), typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) false, new PropertyChangedCallback(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.OnIsContextMenuOpenChanged)));
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.ModeProperty = DependencyProperty.Register(nameof (Mode), typeof (SplitButtonMode), typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) SplitButtonMode.Split));
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.PlacementProperty = ContextMenuService.PlacementProperty.AddOwner(typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) PlacementMode.Bottom, new PropertyChangedCallback(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.OnPlacementChanged)));
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.PlacementRectangleProperty = ContextMenuService.PlacementRectangleProperty.AddOwner(typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) Rect.Empty, new PropertyChangedCallback(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.OnPlacementRectangleChanged)));
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.HorizontalOffsetProperty = ContextMenuService.HorizontalOffsetProperty.AddOwner(typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) 0.0, new PropertyChangedCallback(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.OnHorizontalOffsetChanged)));
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.VerticalOffsetProperty = ContextMenuService.VerticalOffsetProperty.AddOwner(typeof (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton), (PropertyMetadata) new FrameworkPropertyMetadata((object) 0.0, new PropertyChangedCallback(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.OnVerticalOffsetChanged)));
  }

  public override void OnApplyTemplate()
  {
    base.OnApplyTemplate();
    if (!(this.Template.FindName("PART_DropDown", (FrameworkElement) this) is ButtonBase name))
      return;
    name.Click += new RoutedEventHandler(this.Dropdown_Click);
    name.TabIndex = this.TabIndex;
  }

  protected override void OnClick()
  {
    if (this.Mode == SplitButtonMode.Dropdown)
      this.OnDropdown();
    else
      base.OnClick();
  }

  public ItemCollection Items
  {
    get
    {
      this.EnsureContextMenuIsValid();
      return this.ContextMenu.Items;
    }
  }

  public bool IsContextMenuOpen
  {
    get => (bool) this.GetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.IsContextMenuOpenProperty);
    set => this.SetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.IsContextMenuOpenProperty, (object) value);
  }

  public PlacementMode Placement
  {
    get => (PlacementMode) this.GetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.PlacementProperty);
    set => this.SetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.PlacementProperty, (object) value);
  }

  public Rect PlacementRectangle
  {
    get => (Rect) this.GetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.PlacementRectangleProperty);
    set => this.SetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.PlacementRectangleProperty, (object) value);
  }

  public double HorizontalOffset
  {
    get => (double) this.GetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.HorizontalOffsetProperty);
    set => this.SetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.HorizontalOffsetProperty, (object) value);
  }

  public double VerticalOffset
  {
    get => (double) this.GetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.VerticalOffsetProperty);
    set => this.SetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.VerticalOffsetProperty, (object) value);
  }

  public SplitButtonMode Mode
  {
    get => (SplitButtonMode) this.GetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.ModeProperty);
    set => this.SetValue(DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton.ModeProperty, (object) value);
  }

  private static void OnIsContextMenuOpenChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton splitButton = (DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton) d;
    splitButton.EnsureContextMenuIsValid();
    if (!splitButton.ContextMenu.HasItems)
      return;
    bool newValue = (bool) e.NewValue;
    if (newValue && !splitButton.ContextMenu.IsOpen)
    {
      splitButton.ContextMenu.IsOpen = true;
    }
    else
    {
      if (newValue || !splitButton.ContextMenu.IsOpen)
        return;
      splitButton.ContextMenu.IsOpen = false;
    }
  }

  private static void OnPlacementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    if (!(d is DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton splitButton))
      return;
    splitButton.EnsureContextMenuIsValid();
    splitButton.ContextMenu.Placement = (PlacementMode) e.NewValue;
  }

  private static void OnPlacementRectangleChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    if (!(d is DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton splitButton))
      return;
    splitButton.EnsureContextMenuIsValid();
    splitButton.ContextMenu.PlacementRectangle = (Rect) e.NewValue;
  }

  private static void OnHorizontalOffsetChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    if (!(d is DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton splitButton))
      return;
    splitButton.EnsureContextMenuIsValid();
    splitButton.ContextMenu.HorizontalOffset = (double) e.NewValue;
  }

  private static void OnVerticalOffsetChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    if (!(d is DnProcessSimulateCommands.RenameTool.SplitButton.SplitButton splitButton))
      return;
    splitButton.EnsureContextMenuIsValid();
    splitButton.ContextMenu.VerticalOffset = (double) e.NewValue;
  }

  private void EnsureContextMenuIsValid()
  {
    if (this.ContextMenu != null)
      return;
    this.ContextMenu = new ContextMenu();
    this.ContextMenu.PlacementTarget = (UIElement) this;
    this.ContextMenu.Placement = this.Placement;
    this.ContextMenu.Opened += (RoutedEventHandler) ((sender, routedEventArgs) => this.IsContextMenuOpen = true);
    this.ContextMenu.Closed += (RoutedEventHandler) ((sender, routedEventArgs) => this.IsContextMenuOpen = false);
  }

  private void OnDropdown()
  {
    this.EnsureContextMenuIsValid();
    if (!this.ContextMenu.HasItems)
      return;
    this.ContextMenu.IsOpen = !this.IsContextMenuOpen;
  }

  private void Dropdown_Click(object sender, RoutedEventArgs e)
  {
    this.OnDropdown();
    e.Handled = true;
  }
}
